﻿using Direct.IDAL.User.Money;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectSell.Model.User;
using DirectSell.Model.Money;
using System.Collections;
using Direct.IDAL.User.Jurisdiction;

namespace DirectSell.BLL.User.Money
{
    /// <summary>
    /// 货币转换业务逻辑层
    /// </summary>
    public class MoneyTransforService :UserService
    {

        #region 日志记录
        protected override string LogFile
        {
            get
            {
                return "货币转换";
            }
        }
        #endregion

        #region 初始化数据
        /// <summary>
        /// 货币转换仓储接口
        /// </summary>
        protected IMoneyTransforRepository _repository { get; set; }


        public  MoneyTransforService(IMoneyTransforRepository repository)
        {
            this._repository = repository;
        }
        #endregion

        #region 操作入口
        /// <summary>
        /// 操作入口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        protected  override UserRespCode Operation<T>(T param)
        {
            var req = param as MoneyTransforReq;

            if (req == null)
            {
                return UserRespCode.传入参数不正确;
            }

            //判断二级密码
            if (!_repository.CheckSpassword(req.UserCode, req.SecondPassWord))
            {
                return UserRespCode.二级密码输入错误;
            }

            Log(req);

            Hashtable hsTable = new Hashtable();
           
            //奖金转电子币
            if (req.Type == 0)
            {
                var result = RealToVirtual(req, ref hsTable);
                if (!result)
                {
                    return UserRespCode.余额不足;
                }
            }
            //提交事务
            if (!_repository.CommitDapperTranLock(hsTable))
            {
                return UserRespCode.失败;
            }
            return UserRespCode.成功;

        }

        #endregion

        #region 奖金转电子币
        /// <summary>
        /// 
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        protected bool RealToVirtual(MoneyTransforReq req,ref Hashtable hstable)
        {
            req.Remark = "奖金转电子币";
            //判断奖金余额
            if (_repository.GetRealMoney(req.UserCode) < req.Amount)
            {
                return false;
            }
            Consumption con = new Consumption()
            {
                 Affair="奖金转电子币(奖金支出)",
                 Amout = req.Amount,
                 CreateDate = req.CreateTime,
                 LogType = 0,
                 Operator = req.UserCode,
                 Relevant = req.UserCode
            };
            Consumption con1 = new Consumption()
            {
                Affair = "奖金转电子币(电子币收入）",
                Amout = req.Amount,
                CreateDate = req.CreateTime,
                LogType = 1,
                Operator = req.UserCode,
                Relevant = req.UserCode
            };
            _repository.InsertRecord(req,ref hstable);
            _repository.InsertConsumption(con, ref hstable);
            _repository.InsertConsumption(con1, ref hstable);
            _repository.RealToVirtual(req.Amount, req.UserCode, ref hstable);
            return true;
        }

        #endregion

    }
}
